package ggze.mps.home.controller;

import ggze.actuator.ActuatorController;
import ggze.mps.home.dao.inter.Small_ShakyDao;
import ggze.mps.home.entity.Small_Shaky;
import ggze.tool.BeanToJson;
import ggze.tool.ReturnBody;
import ggze.tool.TimeUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("Small_ShakyController")
public class Small_ShakyController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private Small_ShakyDao Small_ShakyDaoName;
    @Autowired
    private ActuatorController actuatorController;

    @RequestMapping("add")
    public String add() throws UnsupportedEncodingException {
        String xp=request.getParameter("jon");
        Small_Shaky entity = (Small_Shaky) BeanToJson.jsonTobean(request.getParameter("jon"), Small_Shaky.class);
        ReturnBody returnBody = new ReturnBody();
        entity.setUuid("");
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setCreateTime(time);
            entity.setMethodTime(time);
            actuatorController.execute(entity, "add", Small_ShakyDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("deleted")
    public String deleted() throws UnsupportedEncodingException {
        Small_Shaky entity = (Small_Shaky) BeanToJson.jsonTobean(request.getParameter("jon"), Small_Shaky.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            Small_ShakyDaoName.delete("uuid", entity.getUuid());
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("update")
    public String update() throws UnsupportedEncodingException {
        Small_Shaky entity = (Small_Shaky) BeanToJson.jsonTobean(request.getParameter("jon"), Small_Shaky.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setMethodTime(time);
            actuatorController.execute(entity, "update", Small_ShakyDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("get")
    public String get() throws UnsupportedEncodingException {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            int begin = Integer.parseInt(jsonObject.getString("begin"));
            int end = Integer.parseInt(jsonObject.getString("end"));
            String zdname = jsonObject.getString("zdname");
            String zdval = jsonObject.getString("zdval");
            if ("1".equals(zdval)) {
                List<Small_Shaky> entityLIST = Small_ShakyDaoName.paging(zdname, zdval, begin, end);
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            } else {
                List<Small_Shaky> entityLIST = Small_ShakyDaoName.customs("select * from Small_Shaky where " + zdname + "  like '%" + zdval + "%'");
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            }
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("num")
    public String num() throws UnsupportedEncodingException {
        return String.valueOf(Small_ShakyDaoName.getNum("1", "1"));
    }
}
